package com.zfzs.post.modules.goods.rest;

import com.zfzs.post.aop.log.Log;
import com.zfzs.post.common.bean.ResponseResult;
import com.zfzs.post.modules.goods.dto.GoodsImgDTO;
import com.zfzs.post.modules.goods.service.GoodsWebService;
import com.zfzs.post.modules.goods.service.dto.GoodsCriteria;
import com.zfzs.post.modules.goods.vo.GoodsWebVO;
import com.zfzs.post.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @description: 商品管理
 * @author: ck
 * @create: 2019-11-29 10:16
 **/
@RestController
@RequestMapping("goods_web")
public class GoodsWebController {

    @Autowired
    private GoodsWebService goodsWebService;

    /**
     * 后台商品
     * @param criteria
     * @param page
     * @param size
     * @return
     */
    @Log("后台商品列表")
    @GetMapping("/goods_list")
    public ResponseResult getGoodsOrderPage(GoodsCriteria criteria, Integer page, Integer size){
        Map<String,Object> map = goodsWebService.queryGoodsPageList(criteria,page,size);
        return ResponseResult.ok(map);
    }

    /**
     * 查询详情
     * @param id
     * @return
     */
    @Log("查询详情")
    @GetMapping(value = "/queryGoodsInfo/{id}")
    @PreAuthorize("hasAnyRole('ADMIN','SYSUPLOADEXCEL_ALL','SYSUPLOADEXCEL_CREATE')")
    public ResponseResult queryGoodsInfo(@PathVariable String id) {
        GoodsWebVO goods = goodsWebService.queryGoodsInfo(id);
        return ResponseResult.ok(goods);
    }

    /**
     * 新增
     * @param resources
     * @return
     */
    @Log("新增")
    @PostMapping(value = "/add")
    @PreAuthorize("hasAnyRole('ADMIN','SYSUPLOADEXCEL_ALL','SYSUPLOADEXCEL_CREATE')")
    public ResponseResult create(@Validated @RequestBody GoodsWebVO resources) {
        Long channelId = SecurityUtils.getUserId();
        resources.setChannelId(channelId);
        goodsWebService.saveGoods(resources);
        return ResponseResult.ok();
    }

    /**
     * 修改
     * @param resources
     * @return
     */
    @Log("修改")
    @PutMapping(value = "/update")
    @PreAuthorize("hasAnyRole('ADMIN','SYSUPLOADEXCEL_ALL','SYSUPLOADEXCEL_EDIT')")
    public ResponseResult update(@Validated @RequestBody GoodsWebVO resources) {
        goodsWebService.updateGoods(resources);
        return ResponseResult.ok();
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @Log("删除")
    @DeleteMapping(value = "/delete/{id}")
    @PreAuthorize("hasAnyRole('ADMIN','SYSUPLOADEXCEL_ALL','SYSUPLOADEXCEL_DELETE')")
    public ResponseResult delete(@PathVariable String id) {
        goodsWebService.delete(id);
        return ResponseResult.ok();
    }

    /**
     * 修改轮播图片
     */
    @Log("修改轮播图片")
    @PutMapping(value = "/update_img_list")
    public ResponseResult updateImgList(@Validated @RequestBody GoodsImgDTO dto) {
        goodsWebService.updateImgList(dto);
        return ResponseResult.ok();
    }
}
